package socket;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.Timer;

public class Server extends JFrame implements ActionListener{

	/**
	 * Data Members
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final int FRAME_X = 100;
	private final int FRAME_Y = 100;
	private final int FRAME_WIDTH = 400;
	private final int FRAME_HEIGHT = 300;
	private JButton sendButton;
	private final int BUTTON_X = 280, BUTTON_Y=50, BUTTON_WIDTH=110, BUTTON_HEIGHT=30;
	private Timer timer;
	private JTextArea text;
	private final int TEXT_X = 10, TEXT_Y=30, TEXT_WIDTH=260, TEXT_HEIGHT=200;
	/*
	 * Network connections
	 */
	private int port = 5555;
	private ServerSocket server;
	private Socket client;
	/*
	 * Streams for network connections
	 */
	BufferedReader instream;
	PrintWriter outstream;
	String in_message, out_message;

	
	
	
	
	/**
	 * Constructor
	 * 
	 * @param title
	 */
	public Server(String title){
		setLayout(null);
		setBounds(FRAME_X, FRAME_Y, FRAME_WIDTH, FRAME_HEIGHT);
		
		/*
		 * Add components
		 */
		sendButton = new JButton("Send message");
		sendButton.setBorder(null);
		sendButton.addActionListener( this );
		sendButton.setBounds( BUTTON_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT );
		add( sendButton );

		text = new JTextArea();
		text.setBorder( BorderFactory.createLoweredBevelBorder() );
		text.setBounds( TEXT_X, TEXT_Y, TEXT_WIDTH, TEXT_HEIGHT );
		text.setLineWrap( true );
		add( text );

		/*
		 * We will use a timer to check for messages every second
		 * 1 sec == 1000 msec
		 * What the code below will do: it will call actionPerformed once second
		 */
		timer = new Timer( 1000, this );
		timer.start();
		
		setTitle(title);
		setVisible( true );
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		/*
		 * Set up network connections
		 */
		try{
			server = new ServerSocket( port );
			client = server.accept();
			
			System.out.println(" Accepted connection from " + 
					client.getInetAddress() + ":" + client.getPort() +
					" local port: " + client.getLocalPort());
			
			InputStreamReader ins = new InputStreamReader( client.getInputStream() );
			instream = new BufferedReader( ins );
			outstream = new PrintWriter( client.getOutputStream(), true );
			
		} catch(IOException ioe){
			System.out.println("Not able to listen on port " + port);
		}
	}
	
	/**
	 * main method
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		new Server("Test Server");
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		/*
		 * User clicks a button:
		 */
		if (arg0.getSource().equals( sendButton )){
			System.out.println("Send button clicked");			
			out_message = text.getText();
			if (outstream != null)
				outstream.println( out_message );
		}
		
		/*
		 * Timer went off
		 */
		if (arg0.getSource().equals( timer )){
			System.out.print(".");
			try {
				if (instream != null && instream.ready())
					in_message = instream.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (in_message != null){
				text.append( "Message from " + 
						client.getInetAddress() + ":" + client.getPort() + "> " +
						in_message + "\n" );
				in_message = null;
			}
		}
	}

}
